package com.epoint.action;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.epoint.common.Result;
import com.epoint.entity.BorrowInfo;
import com.epoint.service.BookInfoService;
import com.epoint.service.BorrowInfoService;

/**
 * Servlet implementation class BorrowInfoListAction
 */
@WebServlet("/borrowinfolistaction")
public class BorrowInfoListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
    BorrowInfoService borrowInfoService = new BorrowInfoService();
    BookInfoService bookInfoService = new BookInfoService();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BorrowInfoListAction() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("queryId".equals(method)) {
			// 响应下一个借阅号
			response.getWriter().println(borrowInfoService.getNextBorrowId());
		}else if("updateRemain".equals(method)) {
			// 还书
			String data = request.getParameter("data");
			String returnTime = request.getParameter("returnTime");
			BorrowInfo borrowInfo = JSONObject.parseObject(data,BorrowInfo.class);
			
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			try {
				Date tmp = format.parse(returnTime);
				borrowInfo.setReturnTime(tmp);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			// 修改借阅表归还时间
			int flag = borrowInfoService.updateBorrowInfo(borrowInfo);
			String returnCode = Result.returnCode(flag);
			response.getWriter().print(returnCode);
			
		}else {
			// 列表展示
			String pageIndex = request.getParameter("pageIndex");
			String pageSize = request.getParameter("pageSize");
			String sortField = request.getParameter("sortField");
			String sortOrder = request.getParameter("sortOrder");
			
			// 请求查询方法
			List<BorrowInfo> borrowInfos = borrowInfoService.getBorrowList(Integer.valueOf(pageIndex), Integer.valueOf(pageSize), sortField, sortOrder);
			
			// 总数
			int count = borrowInfoService.getBorrowListCount();
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("total", count);
			resultMap.put("data", borrowInfos);
			// 返回构建的json数据
			String tmp = JSONObject.toJSONStringWithDateFormat(resultMap, "yyyy-MM-dd HH:mm:ss");
			response.getWriter().println(tmp);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
